package com.haha.gulimall.product.service.impl;

import com.haha.gulimall.product.dao.CategoryBrandRelationDao;
import com.haha.gulimall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haha.common.utils.PageUtils;
import com.haha.common.utils.Query;

import com.haha.gulimall.product.dao.BrandDao;
import com.haha.gulimall.product.entity.BrandEntity;
import com.haha.gulimall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
   private CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");
        QueryWrapper<BrandEntity> qw = new QueryWrapper<>();
        if(StringUtils.isNotBlank(key)){
            qw.eq("brand_id",key).or().like("name",key);
        }
        IPage<BrandEntity> page = this.page(new Query<BrandEntity>().getPage(params), qw);
        return new PageUtils(page);
    }

    /**
     * 主表更新，冗余字段也需要同步更新
     * @param brand
     */
    @Override
    @Transactional
    public void updateDetail(BrandEntity brand) {
        BrandEntity byId = this.getById(brand.getBrandId());
        this.updateById(brand);
        if(!brand.getName().equals(byId.getName())){
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
            //todo 其他表的冗余字段也需要更新
        }

    }

}